package com.idroid.domain;

import java.io.IOException;
import java.util.List;
import java.util.Observable;

import com.idroid.database.DatabaseService;
import com.idroid.fragments.MapFragment;

import android.app.Activity;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

public class LocationTracker extends Observable {

	static Activity activity = null;
	static Location currentLocation = new Location("");

	private LocationTracker() {
		currentLocation.setLatitude(44.9444);
		currentLocation.setLongitude(-93.0931);

		// Acquire a reference to the system Location Manager
		LocationManager locationManager = (LocationManager) activity
				.getSystemService(Context.LOCATION_SERVICE);

		// Define a listener that responds to location updates
		LocationListener locationListener = new LocationListener() {
			public void onLocationChanged(Location location) {
				System.out.println("Updated Location "+location);
				currentLocation = location;
				
				LocationTracker.super.setChanged();
				LocationTracker.super.notifyObservers(location);
			}

			public void onStatusChanged(String provider, int status,
					Bundle extras) {
			}

			public void onProviderEnabled(String provider) {
			}

			public void onProviderDisabled(String provider) {
			}
		};

		// Register the listener with the Location Manager to receive location
		// updates
		try {
			locationManager.requestLocationUpdates(
					LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
		} catch (Exception e) {
			e.printStackTrace();
		}

		try {
			locationManager.requestLocationUpdates(
					LocationManager.GPS_PROVIDER, 0, 0, locationListener);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static LocationTracker instance = null;

	public static void init(Activity activityRef) {
		activity = activityRef;
	}

	public static synchronized LocationTracker getInstance() throws Exception {

		if (activity == null) {
			throw new Exception("activity cannot be null.  Call init()");
		}

		if (instance == null) {
			instance = new LocationTracker();
		}
		return instance;
	}

	public Location getCurrentLocation() {
		return currentLocation;
	}
	
	public ParkingArea setAreaPosition(Activity activity, ParkingArea area) {
		
		Geocoder coder = new Geocoder(activity);

		List<Address> address = null;
		// System.out.println(area.getAddress() + ", "
		// + area.getCity() + ", "
		// + area.getState() + " "
		// + area.getZipCode());
		try {
			address = coder.getFromLocationName(
					area.getAddress() + ", " + area.getCity() + ", "
							+ area.getState() + " " + area.getZipCode(), 1);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (address != null && address.size() > 0) {
			Address addr = address.get(0);
			// System.out.println(addr);
			area.setLatitude(addr.getLatitude());
			area.setLongitude(addr.getLongitude());
			area.setHasPosition(true);
			DatabaseService.getInstance().updateParkingArea(
					activity, area);

		}

		return area;
	}


}
